// src/api/auth/index.ts
import request from '@/utils/request';
import type { UserInfo, UserQuery,UserPageV0,UserForm } from './model';

  /**
   * 登录成功后获取用户信息（昵称、头像、权限集合和角色集合）
   */
export function getInfo(){
    return request<any,UserInfo>({
        url:'/api/v1/users/me',
        method:"get",
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}

/**
   * 获取用户分页列表
   *
   * @param queryParams
   */
export function getPage(queryParams:UserQuery){
    return request<any,UserPageV0[]>({
        url:"/api/v1/users/page",
        method:"get",
        params:queryParams,
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}

  /**
   * 获取用户表单详情
   *
   * @param userId
   */
export function getFormData(userId:number){
    return request<any,UserForm>({
        url:"/api/v1/users/" + userId + "/form",
        method:"get",
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}

/**
   * 添加用户
   *
   * @param data
   */
export function add(data:UserForm){
    return request({
        url:"/api/v1/users",
        method:"post",
        data:data,
        headers:{
            "Content-Type":"application/json"
        }
    })
}

/**
   * 修改用户
   *
   * @param id
   * @param data
   */
export function update(id:number,data:UserForm){
    return request({
        url:"/api/v1/users/" + id,
        method:"put",
        data:data,
        headers:{
            "Content-Type":"application/json"
        }
    })
}

/**
   * 修改用户密码
   *
   * @param id
   * @param password
   */
export function updatePassword(id:number,password:string){
    return request({
        url:"/api/v1/users/" + id + "/password",
        method:"patch",
        params:{password:password},
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}

  /**
   * 删除用户
   *
   * @param ids
   */
  export function deleteByIds(ids:string){
    return request({
        url:"/api/v1/users/" + ids,
        method:"delete",
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}

  /**
   * 下载用户导入模板
   *
   * @returns
   */
  export function downloadTemplate(){
    return request({
        url:"/api/v1/users/template",
        method:"get",
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}

/**
   * 导出用户
   *
   * @param queryParams
   * @returns
   */
export function exports(queryParams:UserQuery){
    return request({
        url:"/api/v1/users/export",
        method:"get",
        params:queryParams,
        responseType:'arraybuffer',
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}

/**
   * 导入用户
   *
   * @param file
   */
export function imports(deptId:number,file:File){
    const formData = new FormData();
    formData.append("file",file);
    return request({
        url:"/api/v1/users/import",
        method:"post",
        params:{deptId:deptId},
        data:formData,
        headers:{
            "Content-Type":"application/x-www-form-urlencoded"
        }
    })
}